package leetcode.stack_queue;

import java.util.ArrayDeque;
import java.util.Comparator;

public class StockSpanner {

    ArrayDeque<int[]> deque;
    int size = 0;

    public StockSpanner() {
        deque = new ArrayDeque<>();
    }

    public int next(int price) {
        size++;
        while (!deque.isEmpty() && deque.peekLast()[0] <= price) {
            deque.pollLast();
        }
        int idx = deque.isEmpty() ? 0 : deque.peekLast()[1];
        deque.offerLast(new int[]{price, size});
        return size - idx;
    }


    public static void main(String[] args) {
        StockSpanner stockSpanner = new StockSpanner();
        System.out.println(stockSpanner.next(100));
        System.out.println(stockSpanner.next(80));
        System.out.println(stockSpanner.next(60));
        System.out.println(stockSpanner.next(70));
        System.out.println(stockSpanner.next(60));
        System.out.println(stockSpanner.next(75));
        System.out.println(stockSpanner.next(85));
    }

}
